package cn.bytecollege.controller;

import cn.bytecollege.model.Cart;
import cn.bytecollege.model.Goods;
import cn.bytecollege.model.Orders;
import cn.bytecollege.model.User;
import cn.bytecollege.service.CartService;
import cn.bytecollege.service.GoodsService;
import cn.bytecollege.service.OrdersService;
import cn.bytecollege.vo.CartVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/cart")
@Log4j2
public class CartController {
    @Autowired
    private CartService cartService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrdersService ordersService;
    @GetMapping("/toCart")
    public String toCart(Model model,
                         HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<CartVO> cartVOS = cartService.getCarts(user.getUserId());
        log.info(cartVOS.toString());
        model.addAttribute("carts",cartVOS);
        return "cart";
    }

    @RequestMapping("/addCart")
    @Transactional
    public String addCart(Integer goodsId,
                          Integer goodsNumber,
                          HttpSession session) {
        log.info(goodsId);
        log.info(goodsNumber);

        User user= (User) session.getAttribute("user");
        Cart cart = new Cart();
        cart.setUserId(user.getUserId());
        cart.setGoodsNumber(goodsNumber);
        cart.setGoodsId(goodsId);
        cartService.addCart(cart);
        return "redirect:/cart/toCart";
    }

    @GetMapping("/modCart")
    public String modCartNumber(HttpSession session,
                                Integer cartId,
                                Integer goodsNumber) {
        Cart cart = new Cart();
        cart.setCartId(cartId);
        cart.setGoodsNumber(goodsNumber);
        cart.setUserId((Integer) session.getAttribute("userId"));
        return "redirect:/toCart";
    }

    @GetMapping("/delCart")
    public String delCart(Integer cartId) {
        cartService.delCart(cartId);
        return "redirect:/cart/toCart";
    }

    @RequestMapping("/chooseAddress")
    public String addOrdersFromCart(HttpSession session,
                                    Integer cartId,
                                    Model model) {
        User user = (User) session.getAttribute("user");
        Cart cart = cartService.getCart(cartId);
        model.addAttribute("cartId", cartId);
        model.addAttribute("addresses", ordersService.getAllAddress(user.getUserId()));
        return "ordersPage";
    }

    @RequestMapping(value = "/addOrdersFromCartAndTOPay",method = RequestMethod.POST)
    @Transactional
    public String addOrdersFromCartAndTOPay(HttpSession session,
                                    Integer userAddressId,
                                    Integer cartId) {

        User user = (User) session.getAttribute("user");
        Orders orders = Orders.builder().userId(user.getUserId()).orderState("待支付").orderDate(new Date()).userAddressId(userAddressId).build();
        HashMap<Integer, Integer> map = new HashMap<>();
        Cart cart = cartService.getCart(cartId);
        map.put(cart.getGoodsId(), cart.getGoodsNumber());
        Integer ordersId = cartService.addOrders(orders, map);
        goodsService.modGoodsStock(cart.getGoodsId(), cart.getGoodsNumber());
        cartService.delCart(cartId);
        return "redirect:/pay/toPay?ordersId="+ordersId;
    }


    @RequestMapping("/addOrdersFromCarts")
    @Transactional
    public String addOrdersFromCarts(HttpSession session,
                                    Integer userAddressId,
                                    Integer... cartIds) {

        log.info(Arrays.toString(cartIds));
        Integer userId = (Integer) session.getAttribute("userId");
        Orders orders = Orders.builder().userId(userId).orderState("待支付").orderDate(new Date()).userAddressId(userAddressId).build();
        HashMap<Integer, Integer> map = new HashMap<>();

        for (Integer cartId : cartIds) {
            Cart cart = cartService.getCart(cartId);
            map.put(cart.getGoodsId(), cart.getGoodsNumber());
        }
        Integer ordersId = cartService.addOrders(orders, map);
        for (Integer cartId : cartIds) {
            cartService.delCart(cartId);
        }
        return "redirect:/pay/toPay?ordersId="+ordersId;
    }
}
